package com.andnux.android.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.andnux.android.databinding.ActivityWelcomeBinding;
import com.andnux.android.dialog.DialogFactory;

public class WelcomeActivity extends BaseActivity<ActivityWelcomeBinding> {

    @Override
    @SuppressLint("ApplySharedPref")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        String url = preferences.getString("url", "");
        if (TextUtils.isEmpty(url)) {
            DialogFactory.showInputDialog(this, "温馨提示",
                    "", "请输入服务器IP地址", (dialog, content) -> {
                        if (TextUtils.isEmpty(content)) {
                            Toast.makeText(this, "请输入服务器IP地址", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        preferences.edit().putString("url", content).commit();
                        startActivity(new Intent(this, LoginActivity.class));
                        dialog.dismiss();
                    });
        } else {
            startActivity(new Intent(this, LoginActivity.class));
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }
}